//
//  MineShopTableViewCell.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/6.
//

#import "MineShopTableViewCell.h"
#import "YPLabel.h"

@interface MineShopTableViewCell ()

@property (nonatomic, strong) UIView *contentBgView;
@property (nonatomic, strong) UIView *avatarBg;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) YPLabel *contentLabel;
@property (nonatomic, strong) UIButton *deleteBtn;
/// 下架
@property (nonatomic, strong) UIButton *downBtn;
/// 置顶
@property (nonatomic, strong) UIButton *topBtn;
@property (nonatomic, strong) UIImageView *topImageV;

@property (nonatomic, strong) UIButton *editBtn;
@property (nonatomic, strong) UIButton *shouqingBtn;
@property (nonatomic, strong) UIButton *updateBtn;
@property (nonatomic, strong) UIButton *qrcodeBtn;

@property (nonatomic, strong) UIView *updateView;

/// 编辑转图
@property (nonatomic, strong) UIButton *editTransBtn;
/// 一键转图
@property (nonatomic, strong) UIButton *forwardBtn;
@property (nonatomic, strong) UILabel *priceLabel;

@end

@implementation MineShopTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
        self.backgroundColor = [UIColor clearColor];
        UIView *v = [[UIView alloc] init];
        v.backgroundColor = [UIColor clearColor];
        self.selectedBackgroundView = v;
        self.tintColor = kBgColor;
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    [self.contentView addSubview:self.contentBgView];
    [self.contentBgView addSubview:self.avatarBg];
    [self.contentBgView addSubview:self.timeLabel];
    [self.contentBgView addSubview:self.contentLabel];
    [self.contentBgView addSubview:self.deleteBtn];
    [self.contentBgView addSubview:self.downBtn];
    [self.contentBgView addSubview:self.topBtn];
    [self.contentBgView addSubview:self.editBtn];
    [self.contentBgView addSubview:self.shouqingBtn];
    [self.contentBgView addSubview:self.updateBtn];
    [self.contentBgView addSubview:self.qrcodeBtn];
    [self.contentBgView addSubview:self.editTransBtn];
    [self.contentBgView addSubview:self.forwardBtn];
    [self.contentBgView addSubview:self.priceLabel];
    [self.avatarBg addSubview:self.topImageV];
    [self.avatarBg addSubview:self.updateView];
    
    [self.contentBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(16);
        make.trailing.mas_equalTo(-16);
        make.top.mas_equalTo(10);
        make.height.mas_equalTo(170);
    }];
    [self.avatarBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(100);
        make.top.leading.mas_equalTo(10);
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.avatarBg.mas_trailing).offset(6);
        make.trailing.mas_equalTo(-10);
        make.top.mas_equalTo(10);
        make.height.mas_equalTo(60);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.avatarBg);
        make.leading.equalTo(self.contentLabel);
    }];
    [self.qrcodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(22);
        make.trailing.mas_equalTo(-10);
        make.centerY.equalTo(self.timeLabel);
    }];
    [self.updateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(64, 24));
        make.trailing.mas_equalTo(self.qrcodeBtn.mas_leading).offset(-10);
        make.centerY.equalTo(self.qrcodeBtn);
    }];
    
    [self.topImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(34);
        make.leading.top.mas_equalTo(0);
    }];
    
    CGFloat w = (kWidthForScreen - 92)/5;
    [self.shouqingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(w, 24));
        make.bottom.mas_equalTo(-15);
    }];
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.shouqingBtn.mas_trailing).offset(10);
        make.size.bottom.equalTo(self.shouqingBtn);
    }];
    [self.topBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.editBtn.mas_trailing).offset(10);
        make.size.bottom.equalTo(self.shouqingBtn);
    }];
    [self.downBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.topBtn.mas_trailing).offset(10);
        make.size.bottom.equalTo(self.shouqingBtn);
    }];
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.downBtn.mas_trailing).offset(10);
        make.size.bottom.equalTo(self.shouqingBtn);
    }];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.top.mas_equalTo(self.avatarBg.mas_bottom).offset(20);
    }];
    [self.forwardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-10);
        make.size.mas_equalTo(CGSizeMake(w, 24));
        make.centerY.equalTo(self.priceLabel);
    }];
    [self.editTransBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.forwardBtn.mas_leading).offset(-10);
        make.size.centerY.equalTo(self.forwardBtn);
    }];
}

- (void)setModel:(ShopModel *)model {
    _model = model;
    self.timeLabel.text = [NSDate formateData:model.createdAt];
    self.contentLabel.text = model.goodsIntroduction;
    [self.downBtn setTitle:model.enabled == 1 ? @"下架" : @"上架" forState:UIControlStateNormal];
    [self.topBtn setTitle:model.top ? @"取消" : @"置顶" forState:UIControlStateNormal];
    [self.topBtn setTitleColor:model.top ? kRGB(0xFF9754) : [UIColor whiteColor] forState:UIControlStateNormal];
    self.topBtn.backgroundColor = model.top ? [UIColor whiteColor] : kRGB(0xFF9754);
    self.topBtn.layer.borderColor = kRGB(0xFF9754).CGColor;
    self.topBtn.layer.borderWidth = model.top ? 1 : 0;
    self.topImageV.hidden = !model.top;
    
    self.updateBtn.hidden = self.updateView.hidden = !model.hasUpdate;
    self.shouqingBtn.hidden = self.editBtn.hidden = self.topBtn.hidden = self.downBtn.hidden = self.deleteBtn.hidden = self.qrcodeBtn.hidden = !model.isSelf;
    
    if (!model.isSelf && !model.trans) {
        self.editTransBtn.hidden = self.forwardBtn.hidden = self.priceLabel.hidden = NO;
    }else {
        self.editTransBtn.hidden = self.forwardBtn.hidden = self.priceLabel.hidden = YES;
    }
    self.priceLabel.hidden = model.isSelf;
    self.priceLabel.text = [NSString stringWithFormat:@"¥ %@",model.sellingPrice];
    
    if (model.isSelf) {
        [self.updateBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(64, 24));
            make.trailing.mas_equalTo(self.qrcodeBtn.mas_leading).offset(-10);
            make.centerY.equalTo(self.qrcodeBtn);
        }];
    }else {
        [self.updateBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(64, 24));
            make.trailing.mas_equalTo(self.editTransBtn.mas_leading).offset(-10);
            make.centerY.equalTo(self.editTransBtn);
        }];
    }
    
    NSMutableArray *imageArr = [NSMutableArray arrayWithArray:model.imgThumbnailUrl];
    if (model.videoPicUrl.length > 0) {
        [imageArr addObject:model.videoPicUrl];
    }
    [self creatAvatar:imageArr];
}

#pragma mark - 懒加载
- (UIView *)contentBgView {
    if (!_contentBgView) {
        _contentBgView = [[UIView alloc] init];
        _contentBgView.backgroundColor = [UIColor whiteColor];
        _contentBgView.layer.cornerRadius = 8;
        _contentBgView.layer.borderColor = [UIColor whiteColor].CGColor;
        _contentBgView.layer.borderWidth = 2;
        _contentBgView.layer.masksToBounds = YES;
    }
    return _contentBgView;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = kFontRegula(12);
        _timeLabel.textColor = kRGB(0x999999);
        [_timeLabel sizeToFit];
    }
    return _timeLabel;
}

- (UIView *)avatarBg {
    if (!_avatarBg) {
        _avatarBg = [[UIView alloc] init];
        _avatarBg.backgroundColor = [UIColor whiteColor];
        _avatarBg.layer.cornerRadius = 10;
        _avatarBg.layer.masksToBounds = YES;
    }
    return _avatarBg;
}

- (YPLabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[YPLabel alloc] init];
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        [_contentLabel setVerAlignment:VerticalAlgnment_top];
        _contentLabel.numberOfLines = 0;
        _contentLabel.textColor = kRGB(0x666666);
        _contentLabel.font = kFontRegula(13);
    }
    return _contentLabel;
}

- (UIButton *)shouqingBtn {
    if (!_shouqingBtn) {
        _shouqingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shouqingBtn setTitle:@"售罄" forState:UIControlStateNormal];
        [_shouqingBtn setTitleColor:kBgColor forState:UIControlStateNormal];
        _shouqingBtn.titleLabel.font = kFontMediium(12);
        _shouqingBtn.backgroundColor = kRGB_ALPHA(0xFF9754, 0.2);
        _shouqingBtn.layer.cornerRadius = 12;
        _shouqingBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_shouqingBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.shouqingBlock) {
                self.shouqingBlock(self.model);
            }
        }];
    }
    return _shouqingBtn;
}

- (UIButton *)editBtn {
    if (!_editBtn) {
        _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _editBtn.backgroundColor = kRGB(0xFF6081);
        [_editBtn setTitle:@"编辑" forState:UIControlStateNormal];
        [_editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _editBtn.titleLabel.font = kFontMediium(12);
        _editBtn.layer.cornerRadius = 12;
        _editBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_editBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.editBlock) {
                self.editBlock(self.model);
            }
        }];
    }
    return _editBtn;
}

- (UIButton *)topBtn {
    if (!_topBtn) {
        _topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _topBtn.backgroundColor = kBgColor;
        [_topBtn setTitle:@"置顶" forState:UIControlStateNormal];
        [_topBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _topBtn.titleLabel.font = kFontMediium(12);
        _topBtn.layer.cornerRadius = 12;
        _topBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_topBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.topBlock) {
                self.topBlock(self.model);
            }
        }];
    }
    return _topBtn;
}

- (UIImageView *)topImageV {
    if (!_topImageV) {
        _topImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_top"]];
    }
    return _topImageV;
}

- (UIButton *)downBtn {
    if (!_downBtn) {
        _downBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _downBtn.backgroundColor = kRGB(0xF2F2F2);
        [_downBtn setTitle:@"下架" forState:UIControlStateNormal];
        [_downBtn setTitleColor:kRGB(0x333333) forState:UIControlStateNormal];
        _downBtn.titleLabel.font = kFontMediium(12);
        _downBtn.layer.cornerRadius = 12;
        _downBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_downBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.topDownBlock) {
                self.topDownBlock(self.model);
            }
        }];
    }
    return _downBtn;
}

- (UIButton *)deleteBtn {
    if (!_deleteBtn) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteBtn.backgroundColor = [UIColor whiteColor];
        [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
        [_deleteBtn setTitleColor:kRGB(0xF15757) forState:UIControlStateNormal];
        _deleteBtn.titleLabel.font = kFontMediium(12);
        _deleteBtn.layer.cornerRadius = 12;
        _deleteBtn.layer.borderColor = kRGB(0xF15757).CGColor;
        _deleteBtn.layer.borderWidth = 1;
        _deleteBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_deleteBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.deleteBlock) {
                self.deleteBlock(self.model);
            }
        }];
    }
    return _deleteBtn;
}

- (UIButton *)qrcodeBtn {
    if (!_qrcodeBtn) {
        _qrcodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_qrcodeBtn setImage:[UIImage imageNamed:@"goods_qr"] forState:UIControlStateNormal];
        @weakify(self)
        [[_qrcodeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.goodsQrcodeBlock) {
                self.goodsQrcodeBlock(self.model);
            }
        }];
    }
    return _qrcodeBtn;
}

- (UIButton *)updateBtn {
    if (!_updateBtn) {
        _updateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _updateBtn.backgroundColor = kRGB(0xFFD937);
        [_updateBtn setTitle:@"同步更新" forState:UIControlStateNormal];
        [_updateBtn setTitleColor:kRGB(0x333333) forState:UIControlStateNormal];
        _updateBtn.titleLabel.font = kFontMediium(12);
        _updateBtn.layer.cornerRadius = 12;
        _updateBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_updateBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.updateBlock) {
                self.updateBlock(self.model);
            }
        }];
    }
    return _updateBtn;
}

- (UIView *)updateView {
    if (!_updateView) {
        _updateView = [[UIView alloc] initWithFrame:CGRectMake(0, 54, 100, 46)];
        _updateView.backgroundColor = kRGB_ALPHA(0x000000, 0.5);
        [_updateView addBottomCorner:8];
        
        UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_update"]];
        [_updateView addSubview:icon];
        [icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(12);
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(7);
        }];
        
        UILabel *label = [[UILabel alloc] init];
        label.textColor = kRGB(0xFFD937);
        label.text = @"有内容更新";
        label.font = kFontMediium(12);
        [_updateView addSubview:label];
    }
    return _updateView;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.textColor = kRGB(0xF15757);
        _priceLabel.font = kFontMediium(16);
        [_priceLabel sizeToFit];
    }
    return _priceLabel;
}

- (UIButton *)forwardBtn {
    if (!_forwardBtn) {
        _forwardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_forwardBtn setTitle:@"一键转图" forState:UIControlStateNormal];
        [_forwardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _forwardBtn.titleLabel.font = kFontMediium(12);
        _forwardBtn.backgroundColor = kBgColor;
        _forwardBtn.layer.cornerRadius = 12;
        _forwardBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_forwardBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.transBlock) {
                self.transBlock(self.model);
            }
        }];
    }
    return _forwardBtn;
}

- (UIButton *)editTransBtn {
    if (!_editTransBtn) {
        _editTransBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_editTransBtn setTitle:@"编辑转图" forState:UIControlStateNormal];
        [_editTransBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _editTransBtn.titleLabel.font = kFontMediium(12);
        _editTransBtn.backgroundColor = kRGB(0xFF6081);
        _editTransBtn.layer.cornerRadius = 12;
        _editTransBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_editTransBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.editTransBlock) {
                self.editTransBlock(self.model);
            }
        }];
    }
    return _editTransBtn;
}

- (void)creatAvatar:(NSMutableArray *)imageArr {
    for (UIView *subview in self.avatarBg.subviews) {
        [subview removeFromSuperview];
    }
    CGFloat w = 100;
    CGFloat h = 100;
    for (int i = 0; i < imageArr.count; i++) {
        UIImageView *imageV = [[UIImageView alloc] init];
        imageV.contentMode = UIViewContentModeScaleAspectFill;
        imageV.clipsToBounds = YES;
        [self.avatarBg addSubview:imageV];
        [imageV sd_setImageWithURL:[NSURL URLWithString:imageArr[i]] placeholderImage:[UIImage imageNamed:@"avatar"]];
        
        if (imageArr.count == 1) {
            [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
        }else if (imageArr.count == 2) {
            [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.mas_equalTo(i*w/2);
                make.top.bottom.mas_equalTo(0);
                make.width.mas_equalTo(w/2);
            }];
        }else if (imageArr.count == 3) {
            /// 1 2 2
            [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                if (i == 0) {
                    make.leading.top.bottom.mas_equalTo(0);
                }else {
                    make.trailing.mas_equalTo(0);
                    make.top.mas_equalTo((i-1)*h/2);
                    make.height.mas_equalTo(h/2);
                }
                make.width.mas_equalTo(w/2);
            }];
        }else if (imageArr.count == 4) {
            int col = i / 2; //0011
            int row = i % 2; //0101
            [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.height.mas_equalTo(w/2);
                make.leading.mas_equalTo(row*w/2);
                make.top.mas_equalTo(col*h/2);
            }];
        }else if (imageArr.count == 5) {
            if (i == 0) {
                [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.width.height.mas_equalTo(w/2);
                    make.top.leading.mas_equalTo(0);
                }];
            }else if (i == 1) {
                [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.width.height.mas_equalTo(w/2);
                    make.bottom.leading.mas_equalTo(0);
                }];
            }else {
                /// 2 3 4
                [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.trailing.mas_equalTo(0);
                    make.top.mas_equalTo((i-2)*h/3);
                    make.width.mas_equalTo(w/2);
                    make.height.mas_equalTo(h/3);
                }];
            }
        }else if (imageArr.count == 6) {
            int row = i / 3; // 000111
            int col = i % 3; // 012012
            [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.mas_equalTo(row*w/3);
                make.top.mas_equalTo(col*h/2);
                make.width.mas_equalTo(w/3);
                make.height.mas_equalTo(h/2);
            }];
        }else if (imageArr.count == 7) {
            /// 3 2 2
            if (i < 3) {
                [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.leading.mas_equalTo(0);
                    make.top.mas_equalTo(h/3*i);
                    make.width.height.mas_equalTo(w/3);
                }];
            }else {
                int row = (i - 3) / 2;
                int col = (i - 3) % 2;
                [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.leading.mas_equalTo(col*w/3+w/3);
                    make.top.mas_equalTo(row*h/2);
                    make.width.mas_equalTo(w/3);
                    make.height.mas_equalTo(h/2);
                }];
            }
        }else if (imageArr.count == 8) {
            /// 2 3 3
            if (i < 2) {
                [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.leading.mas_equalTo(0);
                    make.top.mas_equalTo(h/2*i);
                    make.width.mas_equalTo(w/3);
                    make.height.mas_equalTo(h/2);
                }];
            }else {
                int row = (i - 2) / 2; //0 0  1 1 22
                int col = (i - 2) % 2; //0 1 0 1 0 1
                [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.leading.mas_equalTo(col*w/3+w/3);
                    make.top.mas_equalTo(row*h/3);
                    make.width.height.mas_equalTo(w/3);
                }];
            }
        }else {
            int row = i / 3;
            int col = i % 3;
            [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.mas_equalTo(col*w/3);
                make.top.mas_equalTo(row*h/3);
                make.width.height.mas_equalTo(w/3);
            }];
        }
    }
}

@end
